package com.doublefish.doublefishrobot.ui.home.path.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.List;

/**
 * 作者:dugaofeng on 2025/4/9 16:49
 */
public class PathXYView extends View {

    private List<float[]> matrix;

    //手写画笔
    private final Paint mPaint = new Paint();
    //画笔的路径
    private final Path mPath = new Path();


    public PathXYView(Context context) {
        this(context, null);
    }

    public PathXYView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathXYView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PathXYView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        setBackgroundColor(Color.TRANSPARENT);
    }

    public void setMatrix(List<float[]> matrix) {
        this.matrix = matrix;

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (matrix == null) {
            return;
        }
        mPath.reset();
        //背景颜色设置
//        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        //设置当前笔头颜色
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE); // 设置为划线模式
        //设置当前画笔粗细
        mPaint.setStrokeWidth(5.0f);

        int viewWidth = getWidth();
        float halfWidth = (float) (viewWidth / 2.0f);
        //球台尺寸 长274 宽152
        //宽度用152为基准
        float rate = 100.0f * viewWidth / 152; //与实际的球台的尺寸的倍率

        // FIXME: 2025/4/10 全台的路径 这里不用全台，只要半台
//        mPath.moveTo(halfWidth,0);
//        for (int i = 0; i < matrix.size(); i++) {
//            //设置下一个点
//            float[] point = matrix.get(i);
//            mPath.lineTo(halfWidth + point[0] * rate, point[1] * rate);
//        }
//        canvas.drawPath(mPath, mPaint);

        // FIXME: 2025/4/10 半台的路径 这里不用全台，只要半台
        //半台长1.37m
        float halfLength = 1.37f;
        int i = 0;
        for (i = 0; i < matrix.size(); i++) {
            float[] point = matrix.get(i);
            //球台长2.74m  半台长1.37m
            if (point[1]  >= halfLength) {
                //将画笔移到初始位置
                mPath.moveTo(halfWidth + point[0] * rate,(point[1] - halfLength) * rate);
                break;
            }
        }
        for (; i < matrix.size(); i++) {
            float[] point = matrix.get(i);
            mPath.lineTo(halfWidth + point[0] * rate, (point[1] - halfLength) * rate);//设置下一个点
        }
        canvas.drawPath(mPath, mPaint);

        mPaint.setColor(Color.rgb(1.0f, 0.65f,0.0f));
        mPaint.setStyle(Paint.Style.FILL); // 设置为填充模式
        float[] point = matrix.get(matrix.size() - 1);
        canvas.drawCircle(halfWidth + point[0] * rate, (point[1] - halfLength) * rate, 10, mPaint);
    }
}
